package com.example.day06;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import java.util.List;
import java.util.Map;

public class ExpandableListViewTest extends BaseExpandableListAdapter {
    private  String[] key;
    private Map<String, List<String>> map;
    private Context context;
    private String[] iconNames;

    public ExpandableListViewTest(String[] key, Map<String, List<String>> map, Context context,String[] iconNames) {
        this.key=key;
        this.map = map;
        this.context = context;
        this.iconNames = iconNames;
    }

    @Override
    public int getGroupCount() {
        return map.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return map.get(key[groupPosition]).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return map.get(key[groupPosition]);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return map.get(key[groupPosition]).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        convertView = LayoutInflater.from(context).inflate(R.layout.exp_listview_baseadapter,parent,false);
        TextView ptv = convertView.findViewById(R.id.ptv);
        ptv.setText(key[groupPosition]);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        convertView=LayoutInflater.from(context).inflate(R.layout.exp_listview_child_adapter,parent,false);
        TextView childtv = convertView.findViewById(R.id.childtv);
        ImageView childimg = convertView.findViewById(R.id.childimg);

        String iconName = iconNames[childPosition];
        int iconRes = convertView.getResources().getIdentifier(iconName, "drawable", context.getPackageName());
        Drawable drawableLeft = ContextCompat.getDrawable(context, iconRes);
        childimg.setBackground(drawableLeft);
        childtv.setText(map.get(key[groupPosition]).get(childPosition));
        childtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (childPosition){
                    case 0:
                        Toast.makeText(context, "点击了"+childtv.getText(), Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Dialog alertDialog = new AlertDialog.Builder(v.getContext()).
                                setTitle("对话框的标题").
                                setMessage("对话框的内容").
                                setIcon(R.drawable.ic_launcher_foreground).
                                create();
                        alertDialog.show();

                        break;
                    case 2:
                        TextView textView = new TextView(v.getContext());
                        textView.setText("测试文本");
                        final PopupWindow popupWindow = new PopupWindow(textView,200,300);//参数为1.View 2.宽度 3.高度
                        popupWindow.setOutsideTouchable(true);
                        popupWindow.showAsDropDown(v);
                }

            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
